﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BookManage.BLL;
using BookManage.Model;
using System.Data.SqlClient;

namespace BookManage
{
    public partial class frmSelectBook : Form
    {
        private DataTable dt = null;//存放查询结果，并给DataGridView drvReader提供数据
        private Book book = new Book();
        private BookAdmin bookBLL = new BookAdmin();
        public frmSelectBook()
        {
            InitializeComponent();
        }
        public void ShowData()
        {
            dgvBook.DataSource = dt;
            foreach (DataColumn dc in dt.Columns)
            {
                dgvBook.Columns[dc.ColumnName].HeaderText = Book.ColumnTitle(dc.ColumnName);
            }
        }

        private void BkPreview_Click(object sender, EventArgs e)
        {

        }

        private void BkScamper_Click(object sender, EventArgs e)
        {
            dt = bookBLL.GetAllBook();
            ShowData();
        }

        private void BkQuery_Click(object sender, EventArgs e)
        {
            if (txtbkID.Text == "" && txtbkCode.Text == ""&&txtbkName.Text==""&&txtbkAuthor.Text==""&&txtbkPress.Text=="")
                MessageBox.Show("请输入图书序号或编号进行查询");
            /*else if(book.bkID!=Convert.ToInt32(txtbkID.Text)|| book.bkCode!=txtbkCode.Text)
                MessageBox.Show("该书不存在");*/
            else
            {
                int bkID;
                string bkCode, bkName, bkAuthor, bkPress;

                if (txtbkID.Text.Trim() == "")
                {
                    bkID = 0;
                }
                else
                {
                    int i = txtbkID.Text.IndexOf("--");
                    if (i > 0)
                    {
                        bkID = Convert.ToInt32(txtbkID.Text.Substring(0, i));
                    }
                    else
                    {
                        bkID = Convert.ToInt32(txtbkID.Text);
                    }
                }
                bkCode = txtbkCode.Text;
                bkName = Convert.ToString(txtbkName.Text);
                bkAuthor = Convert.ToString(txtbkAuthor.Text);
                bkPress = Convert.ToString(txtbkPress.Text);
                dt = bookBLL.GetBook(bkID, bkCode, bkName, bkAuthor, bkPress);
                ShowData();
            }
        }

        private void RTBack_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
